package com.example.demo11.controller;

import com.example.demo11.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
public class UserController {

    //用于访问数据库的组件
    @Autowired
    JdbcTemplate jdbcTemplate;
    @RequestMapping("userlist")
 public Object userlist(){
        List<Map<String, Object>> list=jdbcTemplate.queryForList("select * from user");
        System.out.println(list);
     return list;
 }

    @RequestMapping("user")
    public Object user(){
       User u= (User) jdbcTemplate.queryForObject("select * from user where id=1",new BeanPropertyRowMapper<>(User.class));
       // System.out.println(u);
        return u;
    }

//    @RequestMapping("list")
//    public Object list(){
//        User emoor= (User) jdbcTemplate.queryForObject("select * from user where id=1",new BeanPropertyRowMapper<>(User.class));
//        // System.out.println(u);
//        return emoor;
//    }

    @RequestMapping("add")
    public Object add(){
       int ab= jdbcTemplate.update("insert into user (id,name) VALUES (null,?)","胡东");
        // System.out.println(u);
        return ab;
    }

    @RequestMapping("del")
    public Object del(){
        int ab2= jdbcTemplate.update("delete * from user where id=?",7);
        // System.out.println(u);
        return ab2;
    }

    //参数：
    @RequestMapping("users")
    public Object users(@RequestParam String name,@RequestParam String id){
        List<User> list=jdbcTemplate.query("select * from user",new BeanPropertyRowMapper<>(User.class));
        return list;
    }
    @RequestMapping("del1/{id}")
    public Object del1(@PathVariable Integer id){
        String sql="delete * from user where id=?";
        int ab2= jdbcTemplate.update(sql,10);
        // System.out.println(u);
        return ab2;
    }


}
